package com.mycjj.android.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.activity.CarNetworkDetailActivity;
import com.mycjj.android.activity.CarNetworkListActivity;
import com.mycjj.android.activity.CarNetworkMoreThemeActivity;
import com.mycjj.android.activity.CarNetworkMyDetailActivity;
import com.mycjj.android.activity.CarNetworkThemeDetailActivity;
import com.mycjj.android.adapter.CarNetworkAdapter;
import com.mycjj.android.adapter.CarNetworkLastThemeAdapter;
import com.mycjj.android.bean.CarNetworkBaseBean;
import com.mycjj.android.bean.CarNetworkBean;
import com.mycjj.android.bean.CarNetworkLastThemesBaseBean;
import com.mycjj.android.bean.CarNetworkLastThemesBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;


public class CarNetWorkFragment extends BaseFragment implements android.view.View.OnTouchListener{

	private ImageView mHeaderLeftButton;
	private ImageView mHeaderRightButton;
	private TextView mHeaderTitleTextView,tv_mymorenetwork,tv_mymorethemes;

	private Button pop_1,pop_2;

	private ListView car_network_listview,car_network_themes_listview;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = (View) inflater.inflate(R.layout.fragment_car_network, null);
		view.setOnTouchListener(this);
		mPopView = inflater.inflate(R.layout.pop_carnetwork_view, null);
		init(view);
		return view;
	}

	
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
	}



	private void init(View view) {
		
		mHeaderLeftButton = (ImageView)view.findViewById(R.id.header_left_button);
		mHeaderLeftButton.setVisibility(View.GONE);
		mHeaderRightButton = (ImageView) view.findViewById(R.id.header_right_button);
		mHeaderRightButton.setBackgroundResource(R.drawable.iv_right_bg);
		mHeaderRightButton.setVisibility(View.VISIBLE);
		mHeaderTitleTextView = (TextView) view.findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("车圈");
		
		mHeaderRightButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPopWindow();
			}
		});

		pop_1 = (Button) mPopView.findViewById(R.id.pop_1);
		pop_2 = (Button) mPopView.findViewById(R.id.pop_2);

		pop_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), CarNetworkListActivity.class));
				mPopupWindow.dismiss();
			}
		});

		pop_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), CarNetworkMyDetailActivity.class));
				mPopupWindow.dismiss();
			}
		});
		
		car_network_listview = (ListView)view.findViewById(R.id.car_network_listview);
		car_network_themes_listview = (ListView)view.findViewById(R.id.car_network_themes_listview);
		
		car_network_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),CarNetworkDetailActivity.class);
				if(null != networkdataList)
				{
					CarNetworkBean bean = networkdataList.get(position);
					intent.putExtra("id", bean.getId());
				}
				startActivity(intent);
			}
		});
		
		car_network_themes_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),CarNetworkThemeDetailActivity.class);
				if(null != themedataList)
				{
					CarNetworkLastThemesBean bean = themedataList.get(position);
					intent.putExtra("id", bean.getTheme_id());
				}
				startActivity(intent);
			}
		});
		
		tv_mymorenetwork = (TextView) view.findViewById(R.id.tv_mymorenetwork);
		tv_mymorethemes = (TextView) view.findViewById(R.id.tv_mymorethemes);
		
		tv_mymorenetwork.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), CarNetworkListActivity.class));
			}
		});
		
		tv_mymorethemes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), CarNetworkMoreThemeActivity.class));
			}
		});
		
		// 请求数据
		initData();

	}

	List<CarNetworkBean> networkdataList;
	List<CarNetworkLastThemesBean> themedataList;
	private CarNetworkAdapter networkadapter;
	private CarNetworkLastThemeAdapter themeadapter;
	
	private void initData() {
		networkadapter = new CarNetworkAdapter(getActivity());
		themeadapter = new CarNetworkLastThemeAdapter(getActivity());

		//车圈
		requestInterface = new ProjectHttpRequestImpl(
				getActivity(), true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String storeid = Preferences.getSharedPreferences(getActivity(),
				"storeId", "");
		params.put("store_id", storeid);
		params.put("token", Preferences.getSharedPreferences(getActivity(),
				"user_token", ""));
		requestInterface.requestGetsJoinsWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						CarNetworkBaseBean activityBean = ProjectDataManage
								.getInstance().getCarNetworkBaseBean();
						Log.d("CarNetWorkFragment",
								(null == activityBean) ? "CarNetworkBaseBean is null"
										: "activityBean code"
												+ activityBean.getCode());
						if ((null != activityBean)
								&& (null != activityBean.getData())) {
							networkdataList = new ArrayList<CarNetworkBean>();
							if(activityBean.getData().size() > 2)
							{
								networkdataList.add(activityBean.getData().get(0));
								networkdataList.add(activityBean.getData().get(1));
							}
							else
							{
								networkdataList = activityBean.getData();
							}
							networkadapter.setList(networkdataList);
						}
					}

				});

		car_network_listview.setAdapter(networkadapter);
		
		//推荐主题
		requestInterface.requestGetsLastThemesWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						CarNetworkLastThemesBaseBean activityBean = ProjectDataManage
								.getInstance().getCarNetworkLastThemesBaseBean();
						Log.d("CarNetWorkFragment",
								(null == activityBean) ? "CarNetworkLastThemesBaseBean is null"
										: "activityBean code"
												+ activityBean.getCode());
						if ((null != activityBean)
								&& (null != activityBean.getData())) {
							themedataList = activityBean.getData();
							themeadapter.setList(themedataList);
						}
					}

				});

		car_network_themes_listview.setAdapter(themeadapter);
	}

	private PopupWindow mPopupWindow;
	private View mPopView;

	private void showPopWindow() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		} else {
			mPopupWindow = new PopupWindow(mPopView, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			mPopupWindow.setOutsideTouchable(true);
			mPopupWindow.showAsDropDown(mHeaderRightButton, 100, 10);
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if(mPopupWindow != null && mPopupWindow.isShowing()){
			mPopupWindow.dismiss();
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}



	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(mPopupWindow != null && mPopupWindow.isShowing()){
			mPopupWindow.dismiss();
		}
		return false;
	}

}
